package org.ocean.online.utils;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.ocean.online.sys.DbGuard;
import org.ocean.online.sys.DbGuardFactory;
import org.ocean.online.vo.TreeNode;

public class DataLoader {

	public static String loadProjects(Map<String,Object> user){
		DbGuard db = (DbGuard) DbGuardFactory.getDbGuard(user);
		List<Map> projects = db.query("select * from project", null);
//		String json="[{\"text\":\"simple project\"}]";
//		return json;
		return toTreeJsonData(projects);
	}
	
	public static String toTreeJsonData(List<Map> projects){
//		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		JSONArray json = new JSONArray();
//		JSONObject json = new JSONObject();
		for(Map<String,Object> project : projects){
			TreeNode proj = new TreeNode();
			proj.setText(project.get("pname").toString());
			proj.setState("closed");
			proj.getAttributes().put("type", "project");
			json.add(proj);
		}
		
		return json.toString();
	}

	public static String loadCatagory() {
		JSONArray arr = new JSONArray();
		TreeNode model= new TreeNode();
		model.setText("model");
		model.getAttributes().put("type", "model");
		arr.add(model);
		
		TreeNode action= new TreeNode();
		action.setText("action");
		action.getAttributes().put("type", "action");
		arr.add(action);
		
		TreeNode view= new TreeNode();
		view.setText("view");
		view.getAttributes().put("type", "view");
		arr.add(view);
		return arr.toString();
	}
	
	public static String loadModel(Map<String,Object> param){
		DbGuard db = (DbGuard) DbGuardFactory.getDbGuard((Map<String, Object>) param.get("user"));
		
		List<Map> models = db.query("select * from model where \"projectName\"=? and \"modelName\"=?", 
				param.get("projectName"),param.get("modelName"));
		JSONObject jobj = new JSONObject();
		jobj.put("rows", models);
		return jobj.toString();
	}

	private static String toDatagridJson(List<Map<String, Object>> models) {
		// TODO Auto-generated method stub
		return null;
	}

	public static void loadModelData(Map<String, Object> param) {
		DbGuard db = (DbGuard) DbGuardFactory.getDbGuard((Map<String, Object>) param.get("user"));
		
		List<Map> data = db.query("select * from "+param.get("modelName"));
		JSONObject jobj = new JSONObject();
		
	}
}
